package cn.jinjimi.app.main.activity;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.AppManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.GroupEvent;
import cn.jinjimi.app.controller.event.UploadProfileEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.entity.Group;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by haner on 15-1-26.
 */
public class CompanyActivity extends ToolBarCommonActivity implements AdapterView.OnItemClickListener {
    private ListView companyList;
    private Group group;
    private String type;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_city);
        setTitle("公司");
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        type = getIntent().getStringExtra("type");
        companyList = (ListView) findViewById(R.id.area_city_list);
    }

    protected void initListener() {
        companyList.setOnItemClickListener(this);
    }

    protected void initData() {
        if (group == null) {
            getData();
        } else {
            if (group.getCompanies().size() > 0) {
                companyList.setAdapter(new MyAdapter());
            }
        }
    }

    private void getData() {
        showLoadingIndicator();
        UserController.getCompany(type);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return group.getCompanies().size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(CompanyActivity.this, R.layout.item_area_city, null);
            }
            TextView company = (TextView) convertView.findViewById(R.id.area_city);
            company.setText(group.getCompanies().get(position).getCompanyName());
            return convertView;
        }
    }

    private void setCompany(int position) {
        showLoadingIndicator();
        UserController.setCompany(group.getCompanies().get(position).getCompanyId(),
                group.getCompanies().get(position).getCompanyName(), position);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUploadProfileEvent(UploadProfileEvent event) {
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                ToastUtils.toastMessage(CompanyActivity.this, "修改成功");
                AppManager.getInstance().finishActivity(
                        AppManager.getInstance().getActivity(GroupActivity.class));
                finish();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(CompanyActivity.this, "修改失败，请稍后重试");
                break;
        }
        hideLoadingIndicator();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGroupEvent(GroupEvent event) {
        if (isFinishing())
            return;

        switch (event.state) {
            case DataEvent.SUCC:
                group = event.data;
                if (group.getCompanies().size() > 0) {
                    companyList.setAdapter(new MyAdapter());
                }
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(CompanyActivity.this, "网络不给力，请稍后再试");
                break;
        }
        hideLoadingIndicator();
    }
}
